<?php 
namespace Home\Controller;
use Home\Controller\HomeController;
use Think\Page;
use Think\Upload;
use Common\Model\UserModel;
use Common\Controller\CommonController;
class UserController extends CommonController {
    //空操作
    public function _initialize(){
        parent::_initialize();
    }
    public function _empty(){
    	
        header("HTTP/1.0 404 Not Found");
        $this->display('Public:404');
    }
    public function index(){
    
       $member_id=$_SESSION['member_id'];
       $memeber=M('Member')->where('member_id ='.$member_id)->find();
       $data['member']=$memeber;
       $bank=M('Bank')->where('uid ='.$member_id)->find();
      /*  if(!$bank){
       	$data['bank']['bankname']="";
       	$data['bank']['cardnum']="";
       	$data['bank']['cardname']="";
       } */
       $data['bank']=$bank;
       $point=M('Point_orders')->where('member_id ='.$memeber['member_id'])->select();
       foreach ($point as $k=>$v){
       	$point_money+=$v['money'];
       }
       $data['point_money']=$point_money;
       $sale=M('Sale_orders')->where('member_id ='.$memeber['member_id'])->select();
       foreach ($sale as $kk=>$vv){
       	$sale_money+=$vv['money'];
       }
       $data['sale_money']=$sale_money;
       $where['key']="fee";
       $data['withdraw_fee']=M('Config')->where($where)->find()['value'];       
       $this->ajaxReturn($data,'JSONP');
    }
    
    /**
     * 持仓
     */
    public function position(){
    	$member_id=$_SESSION['member_id'];
    	$type=I('type');
    	if(empty(I('type'))){
    		$type=1;
    	}
    	$where['member_id']=$member_id;
    	$where['status']=0;
    	if($type==1){
    		$list=M('Point_orders')->where($where)->select();
    		if($list){
    			$data['list']=$list;
    			$data['status']=1;
    			$this->ajaxReturn($data,'JSONP');
    		}else{
    			$data['list']='';
    			$data['status']=0;
    			$this->ajaxReturn($data,'JSONP');
    		}
    	}
    	if($type==2){
    		$list=M('Sale_orders')->where($where)->select();
    	if($list){
    			$data['list']=$list;
    			$data['status']=1;
    			$this->ajaxReturn($data,'JSONP');
    		}else{
    			$data['list']='';
    			$data['status']=0;
    			$this->ajaxReturn($data,'JSONP');
    		}
    	}
    }
	
    /**
     * 修改交易密码
     */
	  public function updatepwd(){
	  	$oldpwd=I('oldpwd');
	  	$newpwd=I('newpwd');
	  	$member=M('Member')->where('member_id ='.$_SESSION['member_id'])->find();
	  	if(md5($oldpwd)!=$member['pwd']){
	  		$data['status']=0;
	  		$data['info']='旧密码输入错误';
	  		$this->ajaxReturn($data,'JSONP');
	  	}
	  	if(!regex($newpwd, 'password')){
	  		$data['status']=0;
	  		$data['info']='请输入6-20位交易密码';
	  		$this->ajaxReturn($data,'JSONP');
	  	}
	  	$arr['pwd']=md5($newpwd);
	  	$r=M('Member')->where('member_id ='.$_SESSION['member_id'])->save($arr);
	  	if($r){
	  		$data['status']=1;
	  		$data['info']='修改成功';
	  		$this->ajaxReturn($data,'JSONP');
	  	}else{
	  		$data['status']=0;
	  		$data['info']='服务器繁忙，请稍后再试';
	  		$this->ajaxReturn($data,'JSONP');
	  	}
	  }  
    /**
     * 添加密码状态
     */
    public function checkpwd(){
    	$pwd=I('pwd');
    	$member=M('Member')->where('member_id ='.$_SESSION['member_id'])->find();
    	if(md5($pwd)==$member['pwd']){
    		session(array('is_pwd'=>'1','expire'=>3600));
    		$data['status']=1;
    		$data['info']='确认密码成功';
    		$this->ajaxReturn($data,'JSONP');
    	}else{
    		$data['status']=0;
    		$data['info']='交易密码错误';
    		$this->ajaxReturn($data,'JSONP');
    	}
    }
    /**
     * 修改会员信息
     */
    public function updateMassage(){		
       // header("Content-type: text/html; charset=utf-8");
       
        $member_id =$_SESSION['member_id'];
        $name=I('name');
        $idcard=I('idcard');
        if(!regex(I('name'),username)){
        	$data['status']=0;
        	$data['info']='姓名格式不正确';
        	$this->ajaxReturn($data,'JSONP');
        }
        if(!regex(I('idcard'),id_card)){
        	$data['status']=0;
        	$data['info']='身份证格式不正确';
        	$this->ajaxReturn($data,'JSONP');
        }
        if(!regex(I('cardname'),username)){
        	$data['status']=0;
        	$data['info']='持卡人格式不正确';
        	$this->ajaxReturn($data,'JSONP');
        }
        if(!regex(I('cardnum'),bankcard)){
        	$data['status']=0;
        	$data['info']='银行卡号格式不正确';
        	$this->ajaxReturn($data,'JSONP');
        }
        $arr['name']=$name;
        $arr['idcard']=$idcard;
        $arr['status']=1;
        $r[]=M('Member')->where('member_id ='.$member_id)->save($arr);
        $bankname=I('bankname');
        $cardname=I('cardname');
        $cardnum=I('cardnum');
        //$address=I('address');
        $arr2['bankname']=$bankname;
        $arr2['cardname']=$cardname;
        $arr2['cardnum']=$cardnum;
        //$arr2['address']=$address;
        $a=M('Bank')->where('uid = '.$member_id)->find();
        if($a){
        	$r[]=M('Bank')->where('uid = '.$member_id)->save($arr2);
        }else{
        	$arr2['uid']=$member_id;
        	$r[]=M('Bank')->add($arr2);
        }
        if(in_array(false, $r)){
        	$data['status']=0;
        	$data['info']='服务器繁忙，请稍后再试';
        	$this->ajaxReturn($data,'JSONP');
        }else{
        	$data['status']=1;
        	$data['info']='修改成功';
        	$this->ajaxReturn($data,'JSONP');
        }
    }
    /**
     * 进入提现页
     */
    public function checkBank(){
    	$where['key']="fee";
    	$withdraw_fee=M('Config')->where($where)->find()['value'];
    	$member_id=$_SESSION['member_id'];
    	$member=M('Member')->where('member_id ='.$_SESSION['member_id'])->find();
    	$data['fee']=$withdraw_fee;
    	$data['rest']=$member['rmb'];
    	$r=M('Bank')->where('uid ='.$member_id)->find();
    	if(!$r){
    		$data['status']=0;
    		$data['info']='您还没有绑定银行卡，请去完善资料';
    		$this->ajaxReturn($data,'JSONP');
    	}else{
    		$data['status']=1;
    		$data['info']=$r;
    		$this->ajaxReturn($data,'JSONP');
    	}
    }
    /**
     * 申请提现
     */
   public function withdraw(){
	   	$where['key']="fee";
	   	$withdraw_fee=M('Config')->where($where)->find()['value'];
	   	$member_id=$_SESSION['member_id'];
	   	$member=M('Member')->where('member_id ='.$member_id)->find();
	   	$bank=M('Bank')->where('uid ='.$member_id)->find();
	   	$money=I('money');
	   	if($member['rmb']<$money){
	   		$data['status']=0;
	   		$data['info']='余额不足，提现失败';
	   	}
	   	$arr['name']=$member['email'];
	   	$arr['uid']=$member['member_id'];
	   	$arr['all_money']=$money;
	   	$arr['status']=3;
	   	$arr['type']=2;
	   	$arr['address']=$bank['cardnum'];
	   	$arr['bank']=$bank['bankname'];
	   		//应付手续费
        $arr['withdraw_fee'] = floatval(I('money'))*$withdraw_fee*0.01;
            //实际金额
        $arr['money'] = floatval(I('money')) - $arr['withdraw_fee'];
            //加时间
        $arr['add_time'] = time();
            //加订单号
        $arr['order_num'] = $member_id."-".$arr['add_time'];
            //加uid辨明身份
        $arr['uid'] = $member_id;
        $r[] = M('Withdraw')->add($arr);
            //保存可用金额修改信息
        $data_mem['rmb'] = $member['rmb'] - floatval(I('money'));
			//保存冻结金额的修改信息
        $data_mem['forzen_rmb'] = $member['forzen_rmb'] + floatval(I('money'));
        $r[] = M(' ')->where('member_id ='.$member_id)->save($data_mem);          
        if(in_array(false, $r)){
        	$data['status']=0;
        	$data['info']="服务器繁忙，请稍后再试";
        	$this->ajaxReturn($data,'JSONP');
        }else{
        	$data['status']=0;
        	$data['info']="操作成功";
        	$this->ajaxReturn($data,'JSONP');
        }
   }
   
   /**
    * 提现记录
    */
    public function withdrawRecord(){
    	$member_id=$_SESSION['member_id'];
    	$where['uid']=$member_id;
    	$list=M('Withdraw')->where($where)->select();
    	$this->ajaxReturn($list,'JSONP');
    }
    
    /**
     * 充值记录
     */
    public function payRecord(){
    	$member_id=$_SESSION['member_id'];
    	$where['member_id']=$member_id;
    	$list=M('Pay')->where($where)->select();
    	$this->ajaxReturn($list,'JSONP');
    }  
    
	public function chexiaoByid(){
		$withdraw = M('Withdraw');
		$member = M('Member');
		$id = I("post.id");
		if(empty($id)){
			$data['status'] = 0;
			$data['info'] = "参数错误";
			$this->ajaxReturn($data);
		}
		//查询出对应id的提现金额,对应会员的会员id
		$money = $withdraw->field('uid,all_money')->where("withdraw_id = {$id}")->find();
		//对应会员的可用金额和冻结金额
		$rmb = $member->field('rmb,forzen_rmb')->where("member_id = {$money['uid']}")->find();
		//加回可用金额
		$money_back['rmb'] = $rmb['rmb'] + $money['all_money'];
		//减去冻结金额
		$money_back['forzen_rmb'] = $rmb['forzen_rmb'] - $money['all_money'];
		//修改数据库
		$re = $member->where("member_id = {$money['uid']}")->save($money_back);
		if(!$re){
			$data['status'] = 2;
			$data['info'] = "撤销失败";
			$this->ajaxReturn($data);
		}
		$res = $withdraw->where("withdraw_id = {$id}")->delete();
		if(!$res){
			$data['status'] = 3;
			$data['info'] = "撤销失败";
			$this->ajaxReturn($data);
		}
			$data['status'] = 1;
			$data['info'] = "撤销成功";
			$this->ajaxReturn($data);
	}
    
    /**
     * 生成推荐二维码
     */
    public function ercode(){
    	$member=M('Member')->where('member_id = '.$_SESSION['member_id'])->find();
    	$url="http://pan2.money654.com/Home/Reg/checkCode2/code22/".$member['openid'];
    	$code="http://www.gbtags.com/gb/qrcode?t=".$url."&s=400";
    	$this->ajaxReturn($code,'JSONP');
    }
    /**
     * 省级联动
     */
    public function getCity(){
        $area=M("Areas");
        $area_id=intval(I('id'));
        if(!empty($area_id)){
            $city_list=$area->where("parent_id='$area_id'")->select();
//           foreach ($city_list as $vo) {
//               $op[] = '<option value="'.$vo['area_id'].'">'.$vo['area_name'].'</option>'; ;
//           }
//           $this->ajaxReturn($op);
            $this->ajaxReturn($city_list,'JSONP');
        }
    }
    /**
     * ajax上传图片方法
     */
    function addPicForAjax(){
		//dump($_FILES);
		//dump($_GET);
		//dump($_POST);
        //头像上传
        $upload = new Upload();// 实例化上传类
        $upload->maxSize   =     3145728 ;// 设置附件上传大小
        $upload->exts      =     array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $upload->rootPath  =     './Uploads/'; // 设置附件上传根目录
        $upload->savePath  =     'Member/Head/'; // 设置附件上传（子）目录
        // 上传文件
        $info   =   $upload->upload();
        if(!$info) {// 上传错误提示错误信息
            $arr['status']=0;
            $arr['info']=$upload->getError();
            $this->ajaxReturn($arr);
        }else{
            // 上传成功
            $pic = ltrim('/Uploads/'.$info['file_upload1']["savepath"].$info['file_upload1']["savename"],'.');
//            $pic_1=$info['head']['savepath'].$info['head']['savename'];
//            $pic=ltrim($pic_1,".");
            $arr['status']=1;
            $arr['info']=$pic;
            $this->ajaxReturn($arr);
        }
    }

}